/**
 * Captain
 * © 2017 Captain copyright，All rights reserved.
 * http://www.sccaptain.com.cn
 * 
 * JAVA : 8
 * 文  件  名: Criteria.java
 * 创  建  人: liutao (liu.tao@sccaptain.com.cn)
 * 创建时间: 2017年8月2日 下午2:19:55
 * 版         本: 1.0.0
 * 备         注:
 * 修订历史:
 */
package com.material.c2c.core.criteria;

import java.util.ArrayList;
import java.util.List;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.util.StringUtils;

import com.material.c2c.core.criteria.Criterion.BooleanOperator;

/**
 * 查询条件容器。
 * 
 * @since 1.0
 * @version 1.0
 * @author liutao (liu.tao@sccaptain.com.cn)
 */
public class QueryParams {

  // 查询条件容器
  private List<CompoundExpression> expressions = new ArrayList<CompoundExpression>();

  private List<Order> orders = new ArrayList<>();
  
  private String distinct;
  
  public QueryParams and(CriterionExpression criterion) {
    this.expressions.add(new CompoundExpression(BooleanOperator.AND, criterion));
    return this;
  }

  public QueryParams and(CriterionExpression... criterions) {
    this.expressions.add(new CompoundExpression(BooleanOperator.AND, criterions));
    return this;
  }

  public QueryParams or(CriterionExpression criterion) {
    this.expressions.add(new CompoundExpression(BooleanOperator.OR, criterion));
    return this;
  }

  public QueryParams or(CriterionExpression... criterions) {
    this.expressions.add(new CompoundExpression(BooleanOperator.OR, criterions));
    return this;
  }

  public QueryParams orderByDESC(String property) {
    Order order = new Order(Direction.DESC, property);
    this.orders.add(order);
    return this;
  }

  public QueryParams orderByASC(String property) {
    Order order = new Order(Direction.ASC, property);
    this.orders.add(order);
    return this;
  }

  /**
   * 
   * @param sort
   * @return
   * @since 1.0
   * @version 1.0
   * @author liutao (liu.tao@sccaptain.com.cn)
   */
  public QueryParams andOrdersBySort(Sort sort) {
    sort.forEach(order -> {
      this.orders.add(order);
    });
    return this;
  }
  
  /**
   * 添加排序
   * 
   * @param sort
   * @param alias 表别名
   * @return
   * @since 1.0
   * @version 1.0
   * @author liutao (liu.tao@sccaptain.com.cn)
   */
  public QueryParams andOrdersBySort(Sort sort,String alias) {
    sort.forEach(order -> {
      Order o = null;
      if(StringUtils.hasText(alias)){
        if(alias.endsWith(".")){
          o = new Order(order.getDirection(), alias+order.getProperty());
        }else{
          o = new Order(order.getDirection(), alias+"."+order.getProperty());
        }
      }else{
        o = order;
      }
      this.orders.add(o);
    });
    return this;
  }
  
  public QueryParams countDistinct(String distinct){
    this.distinct = " distinct "+ distinct;
    return this;
  }

  public List<CompoundExpression> getExpressions() {
    return expressions;
  }

  public void setExpressions(List<CompoundExpression> expressions) {
    this.expressions = expressions;
  }

  public List<Order> getOrders() {
    return orders;
  }

  public void setOrders(List<Order> orders) {
    this.orders = orders;
  }

  public String getDistinct() {
    return distinct;
  }

  public void setDistinct(String distinct) {
    this.distinct = distinct;
  }

}
